define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function(params) {
        controller.vm = new Vue({
            router: router,
            data: function data() {
                return {
                     
                    page: 1,
                    lastPage:0,
                    mounted: 0,
                    listData: [],
                    nodata:0,
                    pid:95,
                    community_id:0,
                    groupData: {},
                    userInfo: {},
                    imageList: [],
                    imageKeys: [],
                    options:[],
                    choice:1,//单选
                    topicType: 2,
                    type: '',
                    end_date:'',
                    groupData: {},
                    topicData: {},
                    videoSrc: '',
                    videoKey: '',
                    topic_id: 0,
                    chooseVideoText: '选择视频',
                    status: 0,
                    imgsString:"[]",
                    title:'',
                    desc:''

                                 
                };
            },
            el: "#pageVoteAdd",
            created: function created() {
              if(this.$route.params.community_id>0){
                 this.community_id = this.$route.params.community_id;
              }
                this.topic_id = this.$route.params.topic_id;
               
                
                this.getGroup();
                if (this.topic_id > 0) {
                  this.getTopic();
                   
                }

            },

            mounted: function mounted() {
                    if (this.mounted == 1) {
                        return;
                    }
                    this.mounted =1;
                    app.upload("#pageAddUpload");
                    app.lCalendar("end_date");

            } ,
            methods: {
              
                getGroup: function () {
                  var that = this;
                  var id = this.pid;
                  app.get("sns/group/find", {
                    id: id,
                  }, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                    
                    that.setData({ groupData: res.data });
                  })
                },

               switch1Change: function () {
                  var v = $('[name="checkbox1"]').prop("checked")?2:1;
                  this.setData({ choice: v});
                 // console.log('switch1 发生 change 事件，携带值为', e.detail.value)
                },

              getTopic: function () {
                
                var that = this;
                 
                var topic_id = this.topic_id;
                
                app.get("sns/topic/detail", {
                  id: topic_id,
                }, function (res) {
                  if (res.code != 200) {
                    app.alert(res.msg);
                    return;
                  }
                  // that.getPidArray();
                   that.setData({
                    desc: res.data.desc,
                    options: res.data.content_data.options,
                    choice: res.data.content_data.choice,
                   
                    type: res.data.type,
                    pid: res.data.pid,
                    title:res.data.title
              
                  });
                   $("#end_date").val(res.data.content_data.end_date);
                   
                  that.community_id = res.data.shop_id;

                    // 图片
                   if(res.data.pics.length>0){
                     var imgs =[];
                     for (var i  in res.data.pics) {
                         imgs.push({name:'',file_url:res.data.pics[i]});
                     }
                      that.imgsString = JSON.stringify(imgs);
                        that.$nextTick(function(){
                         app.upload($("#pageAddUpload"));
                      });
                   }
                  
                   // 图片完
                   

                 }); 
                  
              },

              //设置时间
              bindDateChange: function (e) {
                var v = e.detail.value;
                var name = e.currentTarget.dataset.name;
                this.setData({ end_date: v });
                
              },


              
              //发布活动 
              formSubmit: function () {
                
                
                var postData = {
                   'title':$("#title").val(),
                   'end_date':$("#end_date").val(),
                   'desc':$("#desc").val(),
                   'id':this.topic_id
                };
                if (this.loading == 1) {
                  return;
                }

                if (postData.title == "" ) {
                  app.alert('请设置标题');
                  return false;
                }
                if (this.options.length==0){
                  app.alert('请设置选项');
                  return false;
                }
                if (postData.end_date=="") {
                  app.alert('请选择投票截止时间');
                  return false;
                }

                

                var groupData = this.groupData;

                this.setData({ 'loading': 1 });
                this.setData({ 'disabled': 1 });
                var _this = this;
                var title = postData.title;
                var imageKeys =[];
                $(".photoList-content li").each(function(i,li){
                   $(li).attr('url') &&  imageKeys.push($(li).attr('url'));
                })

                //提交
                app.post("sns/topic/add", {
                  title: title,
                 
                  desc: postData.desc,
                  pics: imageKeys.join(","),
                  type: "vote",
                  pid: this.pid,
                  id: this.topic_id,
                  
                  content: JSON.stringify({
                    options:this.options,
                    choice: this.choice,
                    end_date: postData.end_date
                  }),
                  
                }, function (res) {
                  _this.setData({ 'loading': 0 });
                  _this.setData({ 'disabled': 0 });
                  var title = "";
                  if (res.code == 200) {
                    

                    var topic_id = res.data.id;
                    _this.suc();
                    
                  } else {
                    app.alert(res.msg);
                  }
                })
              },
              add_option:function(e){
                function randomNumber() {
                  return randomFromInterval(1, 1e6)
                }
                function randomFromInterval(e, t) {
                  return Math.floor(Math.random() * (t - e + 1) + e)
                }
                var option={
                  text:"",
                  id: randomNumber(),
                }
                var options = this.options;
                options.push(option);
                this.setData({ options: options});
              },
              del_option:function(e){
               var index = $(e.target).attr("data-id");
                var options = this.options;
                options.splice(index, 1);
                this.setData({ options: options });
              },
              setOption:function(e){
              
               var index = $(e.target).attr("data-id");
               var options = this.options;
               options[index].text = $(e.target).val();
               this.options = options;
               console.log(this.options);
              },
              suc: function () {
                var _this = this;
                 app.setPageData("vote/index", { reload: 1 });
                app.alert("保存成功",function(){
                    _this.navigateBack();
                })
               
              
              },
              //返回 
              navigateBack: function (e) {
                   this.$router.go(-1);
              },

                setData:function(data){
                     for(var i in data){
                        this[i]=data[i];
                     }
                }
                    
           }
        }).$mount("#app");
    };
    controller.pageShow = function() {};
    module.exports = controller;
});
